// Tierless
// Copyright © 2010 Alphora
// This file is licensed under a modified BSD-license (see tierless_license.txt).

using System;

using Tierless.Framework.Data;
using Tierless.Framework.Entities;
using Tierless.Framework.EntityTypes;
using Tierless.Framework.Server.Data;

namespace Tierless.Framework.Server.Constraints
{
	/// <summary>
	/// Enumerates the possible referential action triggers for cascade and clear reference enforcement.
	/// </summary>
	public enum ReferentialActionTrigger 
	{ 
		/// <summary>
		/// When a target row is updated.
		/// </summary>
		Update, 

		/// <summary>
		/// When a target row is deleted.
		/// </summary>
		Delete 
	};

	/// <summary>
	/// Provides a skeleton for the implementation of referential action handlers.
	/// </summary>
	public class ReferentialActionHandler : ServerEntityEventHandler
	{
		/// <summary>
		/// Initializes a new instance of the ReferentialActionHandler class.
		/// </summary>
		/// <param name="reference">The reference to be managed.</param>
		/// <param name="trigger">The trigger that will be handled.</param>
		public ReferentialActionHandler(EntityReference reference, ReferentialActionTrigger trigger) : base(String.Format("{0}_{1}_Handler", reference.Name, trigger), trigger == ReferentialActionTrigger.Update ? EntityEvent.AfterUpdate : EntityEvent.AfterDelete)
		{
			Reference = reference;
			Trigger = trigger; 
		}

		/// <summary>
		/// Gets the reference to be managed.
		/// </summary>
		public EntityReference Reference { get; private set; }

		/// <summary>
		/// Gets the trigger to be handled.
		/// </summary>
		public ReferentialActionTrigger Trigger { get; private set; }

		public override void Invoke(EntityProvider provider, Entity entity)
		{
			// TODO: Referential action enforcement: Clear and Cascade
		}
	}
}
